package com.tcode.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import com.tcode.bean.PrivilegeBean;
import com.tcode.bean.RoleBean;
import com.tcode.bean.UserBean;
import com.tcode.dao.UserDao;
import com.tcode.listener.ContextLoaderListener;
import com.tcode.utils.BeanFactory;
import com.tcode.utils.Page;

public class UserService {
    
	
	UserDao ud = (UserDao) BeanFactory.getBean("UserDao");
//	UserDao ud = new UserDao();
	
	
	
	public boolean getUserCount(UserBean u) throws Exception{
		boolean flag = false;
		if(ud.getUserByUserNameAndPwd(u)!=0){
			flag = true;
		}
		
		return flag;
	}
	
	
	public boolean getUserCountIfExist(UserBean u) throws Exception{
		
		boolean flag = false;
		//根据前台传入的用户名和密码查询这个用户名是不是已经存在的，如果等于0则用户不存在允许注册。反之不然
		if(ud.getUserByUserNameAndPwd(u)==0){
			flag = true;
		}
		
		return flag;
	}
	
	public UserBean getUserInfo(UserBean u) throws Exception{
		return ud.getUserbeanByUserNameAndPwd(u);
	}
	
	public boolean addUser(UserBean u) throws SQLException{
		boolean flag = false;
		if(ud.insertUser(u)!=0){
			flag = true;
		}
		return flag;
	}
	
	public boolean updateUser(UserBean u)throws Exception{
		boolean flag = false;
		if(ud.modUser(u)!=0){
			flag = true;
		}
		return flag;
	}
	
	public Page<UserBean> getUserList(UserBean u ,int pageNo,int pageSize) throws Exception{
		
		Page<UserBean> page = new Page<UserBean>(pageNo, pageSize);
//		page.setPageNo(pageNo);
		page.setTotalNum(ud.getUserCountBySomething(u));
		List<UserBean> list =ud.getUserList(u, page.getIndexNo(), pageSize);
//		System.out.println(page.getTotalPage());
		page.setList(list);
		return page;
		
	}
	
	public List<UserBean> getUserForExcel() throws SQLException{
		return ud.searchAllUser();
	}
	
	
	public List<UserBean> searchUser() throws SQLException{
		return ud.getAll();
	}
	/**
	 * 根据用户ID获得用户下的所有权限
	 * */
	public List<PrivilegeBean>  getRolePrivilege(int id) throws SQLException{
		
		List<RoleBean> role = ud.getRoleByUserId(id);
		
		Map<String, Object> map = new HashMap<String,Object>();
		
		List<PrivilegeBean> list =new ArrayList<PrivilegeBean>();
		
		for (RoleBean roleBean : role) {
			List<PrivilegeBean> privilege =ud.getPrivilengeByRoleId(roleBean.getId());
			
			for (PrivilegeBean privilegeBean : privilege) {
				map.put(String.valueOf(privilegeBean.getId()),privilegeBean);
			}
		}
		
		for (Object p : map.values()) {
			list.add((PrivilegeBean) p);
		}
		return list;
	}
	
	public List<RoleBean> getRoleByUserid(int id) throws SQLException{
		return ud.getRoleByUserId(id);
	}
	public List<RoleBean> getAllRole() throws SQLException{
		return ud.getAllRole();
	}
	
	public boolean insertUserRole(RoleBean role) throws SQLException {
		boolean flag = false;
		
		if(ud.addUserRole(role)==1) {
			flag = true;
		}
		
		return flag;
	}
	
	public boolean delUserRole(RoleBean role) throws SQLException {
		boolean flag = false;
		
		if(ud.delUserRole(role)==1) {
			flag = true;
		}
		
		return flag;
	}

}
